<template>
  <ul class="list">
    <li
    class="list-item"
    v-for="item of letters"
    :key="item"
    :ref="item"
    @click="handleLetterClick"
    @touchstart.prevent="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
    >{{item}}</li>
  </ul>
</template>

<script>
  export default {
    name: 'cityAlphabet',
    props: {
      alphabetList: Object
    },
    computed: {
      letters (){
        const letters = []
        for (let i in this.alphabetList) {
          letters.push(i)
        }
        return letters
      }
    },
    data (){
      return {
        touchStatus: false,
        startY: 0,
        timer: null
      }
    },
    updated (){
      this.startY = this.$refs['A'][0].offsetTop
    },
    methods: {
      handleLetterClick (e){
        var targetText = e.target.innerText
        this.$emit('clicked', targetText)
      },
      handleTouchStart (){
        this.touchStatus = true
      },
      handleTouchMove (e){
        if (this.touchStatus) {
          if (this.timer) {
            clearTimeout(this.timer)
          }
          this.timer = setTimeout(() => {
            const touchY = e.touches[0].clientY
            const index = Math.floor((touchY - this.startY) / 17.5)
            if (index >= 0 && index < this.letters.length) {
              this.$emit('clicked', this.letters[index])
            }
          },16)
        }
      },
      handleTouchEnd (){
        this.touchStatus = false
      }
    }
  }
</script>

<style lang="stylus" scoped>
  @import '~@S/varibles.styl'
  .list
    position: absolute
    right: .05rem
    top: 0
    height: 100%
    display: flex
    flex-direction: column
    justify-content: center
    z-index: 99
    .list-item
      color: $bgColor
      font-size: .28rem
      line-height: .35rem
      text-align: center
</style>
